package main.ServerTest;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/15 9:45
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class TestUtil {
    public static void createChatRoomsTable(Connection connection) throws SQLException {
        String sql = "CREATE TABLE chatrooms ("
                + "ChatRoomID int NOT NULL,"
                + "ChatRoomName varchar(255) DEFAULT NULL,"
                + "PRIMARY KEY (ChatRoomID)"
                + ")";
        executeUpdate(connection, sql);
    }

    public static void insertTestDataIntoChatRooms(Connection connection) throws SQLException {
        String sql = "INSERT INTO chatrooms (ChatRoomID, ChatRoomName) VALUES "
                + "(16, 'Chat Room 1'), "
                + "(13, 'Chat Room 2'), "
                + "(15, 'Chat Room 3')";
        executeUpdate(connection, sql);
    }

    public static void createUsersTable(Connection connection) throws SQLException {
        String sql = "CREATE TABLE users ("
                + "UserID int NOT NULL,"
                + "Username varchar(255) DEFAULT NULL,"
                + "Password varchar(255) DEFAULT NULL,"
                + "Nickname varchar(255) DEFAULT NULL,"
                + "Avatar varchar(255) DEFAULT NULL,"
                + "PRIMARY KEY (UserID)"
                + ")";
        executeUpdate(connection, sql);
    }

    public static void insertTestDataIntoUsers(Connection connection) throws SQLException {
        String sql = "INSERT INTO users (UserID, Username, Password, Nickname, Avatar) VALUES "
                + "(1, 'john', 'password123', 'John', 'john.jpg'), "
                + "(2, 'alice', 'password456', 'Alice', 'alice.jpg'), "
                + "(3, 'bob', 'password789', 'Bob', 'bob.jpg')";
        executeUpdate(connection, sql);
    }

    private static void executeUpdate(Connection connection, String sql) throws SQLException {
        try (Statement statement = connection.createStatement()) {
            statement.executeUpdate(sql);
        }
    }
}
